package top.buluoluo.productservice.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import top.buluoluo.productservice.dto.response.BrandDTO;
import top.buluoluo.productservice.entity.ProductBrand;

import java.util.List;

@Mapper
public interface BrandMapper extends BaseMapper<ProductBrand> {
    /**
     * 根据分类ID列表查询关联的品牌信息
     * 使用 @Select 注解定义SQL
     */
    @Select("<script>" +
            "SELECT DISTINCT " +
            "   pb.id, " +
            "   pb.name, " +
            "   pb.logo_url as logoUrl, " +
            "   pb.description, " +
            "   pb.status, " +
            "   0 as productCount " + // 暂时设为0，后续可通过其他方式统计
            "FROM product_brand pb " +
            "INNER JOIN product_brand_category pbc ON pb.id = pbc.brand_id " +
            "WHERE pbc.category_id IN " +
            "   <foreach item='categoryId' collection='categoryIds' open='(' separator=',' close=')'>" +
            "       #{categoryId}" +
            "   </foreach> " +
            "AND pb.status = 1 " +
            "ORDER BY pb.name" +
            "</script>")
    List<BrandDTO> selectBrandsByCategoryIds(@Param("categoryIds") List<Long> categoryIds);
}
